home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / xlib04.zip / XMAKEVBM.C < prev    next >
Text File  |  1992-11-12  |  7KB  |  205 lines

  1. /*----------------------------------------------------------------------
  2. ; MODULE XMAKEVBM
  3. ;
  4. ; Implements function to generate a Video bitmap (VBM) from a linear bitmap
  5. ; (LBM)
  6. ;
  7. ; Compile with Tasm.
  8. ; C callable.
  9. ;
  10. ;
  11. ; Based on "CreateMaskedAlignedImage" published in Dr Dobbs Journal
  12. ; by Michael Abrash (Jul - Aug 1991)
  13. ;
  14. ; ****** XLIB - Mode X graphics library                ****************
  15. ; ******                                               ****************
  16. ; ****** Written By Themie Gouthas                     ****************
  17. ; ****** Aeronautical Research Laboratory              ****************
  18. ; ****** Defence Science and Technology Organisation   ****************
  19. ; ****** Australia                                     ****************
  20. ;
  21. ; egg@dstos3.dsto.gov.au
  22. ; teg@bart.dsto.gov.au
  23. ;-----------------------------------------------------------------------*/
  24. /*
  25.   Here is an outline of the XLIB image formats
  26.  
  27.  
  28.   PLANAR BITMAPS
  29.   --------------
  30.  
  31.   Planar bitmaps as used by these functions have the following structure:
  32.  
  33.   BYTE 0                 The bitmap width in bytes (4 pixel groups) range 1..255
  34.   BYTE 1                 The bitmap height in rows range 1..255
  35.   BYTE 2..n1             The plane 0 pixels width*height bytes
  36.   BYTE n1..n2            The plane 1 pixels width*height bytes
  37.   BYTE n2..n3            The plane 2 pixels width*height bytes
  38.   BYTE n3..n4            The plane 3 pixels width*height bytes
  39.  
  40.   LINEAR BITMAPS
  41.   --------------
  42.  
  43.   Linear bitmaps have the following structure:
  44.  
  45.   BYTE 0                 The bitmap width in pixels  range 1..255
  46.   BYTE 1                 The bitmap height in rows   range 1..255
  47.   BYTE 2..n              The width*height bytes of the bitmap
  48.  
  49.  
  50.   VIDEO BITMAPS
  51.   -------------
  52.  
  53.       WORD  0   Size          Total size of this VBM structure in bytes
  54.       WORD  1   ImageWidth    Width in bytes of the image (for all alignments)
  55.       WORD  2   ImageHeight   Height in scan lines of the image
  56.  
  57.       WORD  3 Alignment 0  ImagePtr   Offset in VidRAM of this aligned image
  58.    +--WORD  4              MaskPtr    Offset (within this structure's DS) of
  59.    |   .                   alignment masks
  60.    |   .
  61.    |   .
  62.    |  WORD  9 Alignment 3  ImagePtr   Offset in VidRAM of this aligned image
  63.   +|--WORD 10              MaskPtr    Offset (within this structure's DS) of
  64.   ||                              alignment masks
  65.   ||
  66.   |+->BYTE 21 (WORD 11)                -------+-- Image masks for alignment 0
  67.   |   .                                       |
  68.   |   .                                       |
  69.   |   BYTE  21 + ImageWidth*ImageHeight  -----+
  70.   |
  71.   |   .
  72.   |   . (similaly for alignments 1 - 2 )
  73.   |   .
  74.   |
  75.   +-->BYTE  21 + 3*ImageWidth*ImageHeight + 1-+-- Image masks for alignment 3
  76.       .                                       |
  77.       .                                       |
  78.       BYTE  21 + 4*(ImageWidth*ImageHeight) --+
  79.  
  80.       .
  81.       .
  82.       << Similarly for alignments 2 and 3 >>
  83.       .
  84.       .
  85.       BYTE 21 + 4*(ImageWidth*ImageHeight)
  86.   -------------
  87.  
  88.   (And dont forget the corresponding data in video ram)
  89.  
  90.  
  91. */
  92.  
  93.  
  94.  
  95. #include <alloc.h>
  96. #include <dos.h>
  97.  
  98. /* function to store the linear bitmap in the required video RAM offset */
  99. /* and in the required alignment                                        */
  100.  
  101. extern unsigned int x_store_vbm_image(unsigned int, int, char far *);
  102.  
  103.  
  104. /* Alignment structures, 4 of which make up the header section of the */
  105. /* video bitmap                                                       */
  106.  
  107. typedef struct {
  108.   unsigned int  size;
  109.   unsigned int ImageWidth;
  110.   unsigned int ImageHeight;
  111.   struct {
  112.      unsigned int  ImagePtr;
  113.      unsigned int  MaskPtr;
  114.   } alignments[4];
  115. } alignment_header;
  116.  
  117. /* Structure to extract width/height frol LBM (linear bit map) */
  118.  
  119. typedef struct {
  120.   unsigned char width;
  121.   unsigned char height;
  122. } lbm_header;
  123.  
  124.  
  125. /*************************************************************************/
  126. /*                                                                       */
  127. /* Generates all four possible mode X image/mask alignments, stores      */
  128. /* image alignments in display memory, allocates memory for and generates*/
  129. /* mask alignments, and fills out a VBM aligned masked image structure.  */
  130. /* Each non-zero byte in source bitmap corresponds to image pixel to be  */
  131. /* drawn.                                                                */
  132. /* On success returns a far pointer to the new VBM structure otherwise   */
  133. /* it returns NULL                                                       */
  134. /*                                                                       */
  135. /* Source Language: C                                                    */
  136. /*                                                                       */
  137. /* Parameters:                                                           */
  138. /*    lbm        pointer to linear bitmap                                */
  139. /*    vramStart  contains the next available video offset which is       */
  140. /*               also updated after calling this function                */
  141. /*                                                                       */
  142. /*************************************************************************/
  143.  
  144. char far *x_make_vbm(char far *lbm, unsigned int *VramStart)
  145. {
  146.  
  147.   lbm_header far       *lbm_headr;
  148.   alignment_header far *vbm_headr;
  149.   char far             *vbm_mask_ptr,*p;
  150.   char far             *lbm_pixel_ptr;
  151.   int                  align,BitNum,TempImageWidth;
  152.   unsigned int         TempWidth,TempHeight,TempSize,MaskSize,VramOffs,MaskSpace=0;
  153.   int                  scanline;
  154.   unsigned char        MaskTemp;
  155.  
  156.   VramOffs  = *VramStart;
  157.   lbm_headr = (lbm_header far *) lbm;
  158.  
  159.   TempWidth     = (lbm_headr->width+3)/4+1;
  160.   TempHeight    = lbm_headr->height;
  161.   TempSize      = TempWidth*TempHeight;
  162.  
  163.   vbm_headr = (alignment_header far *) farmalloc(22+TempSize*4);
  164.   if (!vbm_headr) return NULL;
  165.  
  166.   MaskSpace=22;
  167.  
  168.   vbm_headr->ImageWidth  = TempWidth;
  169.   vbm_headr->ImageHeight = TempHeight;
  170.   vbm_headr->size        = 22+TempSize*4;
  171.   for (align=0;align<4;align++){
  172.     vbm_headr->alignments[align].ImagePtr = VramOffs;
  173.     x_store_vbm_image(VramOffs,align,lbm);
  174.     MaskSpace+=TempSize;
  175.     VramOffs+=TempSize;
  176.   }
  177.  
  178.  
  179.   vbm_mask_ptr = (char far *)vbm_headr+22;
  180.  
  181.   for (align=0;align<4;align++){
  182.     lbm_pixel_ptr = lbm + 2;
  183.     vbm_headr->alignments[align].MaskPtr = FP_OFF(vbm_mask_ptr);
  184.     for (scanline=0;scanline<TempHeight;scanline++){
  185.       BitNum=align;
  186.       MaskTemp=0;
  187.       TempImageWidth=lbm_headr->width;
  188.       do {
  189.     MaskTemp |= (*lbm_pixel_ptr++ !=0) << BitNum;
  190.     if (++BitNum > 3) {
  191.       *vbm_mask_ptr++=MaskTemp;
  192.       MaskTemp=BitNum=0;
  193.     }
  194.       } while (--TempImageWidth);
  195.       *vbm_mask_ptr++=(BitNum != 0)?MaskTemp:0;
  196.    }
  197.  
  198.  }
  199.  
  200.  *VramStart=VramOffs;
  201.  return (char far *) vbm_headr;
  202. }
  203.  
  204.  
  205.